package com.credit.jumu.playcard.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.base.BaseActivity;
import com.credit.jumu.playcard.fragment.MessageDiscountFragment;
import com.credit.jumu.playcard.fragment.MessageNoticeFragment;

/**
 * Created by Administrator on 2017/10/21/021.
 */

public class MessageActivity extends BaseActivity implements View.OnClickListener {
	ImageView imageIcon;
	ImageView titleback;
	TextView title, shownotice, showdiscount;
	MessageDiscountFragment messageDiscountFragment;//活动
	MessageNoticeFragment messageNoticeFragment;//通知
	View showline, disline;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		initView();
	}

	private void initView() {
		imageIcon = (ImageView) findViewById(R.id.imageIcon);
		imageIcon.setVisibility(View.GONE);
		titleback = (ImageView) findViewById(R.id.titleback);
		titleback.setVisibility(View.VISIBLE);
		title = (TextView) findViewById(R.id.title);
		showline = findViewById(R.id.showline);
		disline = findViewById(R.id.disline);
		title.setText("消息");
		showNotice();
		backClick(R.id.titleback, MessageActivity.this);
		shownotice = (TextView) findViewById(R.id.shownotice);
		showdiscount = (TextView) findViewById(R.id.showdiscount);
		shownotice.setOnClickListener(this);
		showdiscount.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
			case R.id.shownotice:
				showNotice();
				shownotice.setTextColor(getResources().getColor(R.color.app_them));
				showdiscount.setTextColor(getResources().getColor(R.color.cardcoloer));
				showline.setVisibility(View.VISIBLE);
				disline.setVisibility(View.INVISIBLE);
				break;
			case R.id.showdiscount:
				showDiscount();
				shownotice.setTextColor(getResources().getColor(R.color.cardcoloer));
				showdiscount.setTextColor(getResources().getColor(R.color.app_them));
				showline.setVisibility(View.INVISIBLE);
				disline.setVisibility(View.VISIBLE);
				break;
		}

	}

	//隐藏所有的fragment
	private void hideFragment(FragmentTransaction transaction) {
		if (messageNoticeFragment != null) {
			transaction.hide(messageNoticeFragment);
		}
		if (messageDiscountFragment != null) {
			transaction.hide(messageDiscountFragment);
		}

	}

	private void showDiscount() {//messageDiscountFragment
		FragmentTransaction tr1 = getSupportFragmentManager().beginTransaction();
		if (null == messageDiscountFragment) {
			messageDiscountFragment = new MessageDiscountFragment();
			tr1.add(R.id.messagefeagment, messageDiscountFragment);
		}
		hideFragment(tr1);
		tr1.show(messageDiscountFragment);
		tr1.commit();
	}

	private void showNotice() {//通知messageNoticeFragment
		FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
		if (null == messageNoticeFragment) {
			messageNoticeFragment = new MessageNoticeFragment();
			tr.add(R.id.messagefeagment, messageNoticeFragment);
		}
		hideFragment(tr);
		tr.show(messageNoticeFragment);
		tr.commit();
	}
}
